package com.huicai.poi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huicai.poi.entity.Product;
import com.huicai.poi.mapper.PoiMapper;
import com.huicai.poi.service.PoiService;
import com.huicai.poi.utils.ExcelReadUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author: MR.LIU
 * @description:
 * @date: 2020/6/7
 * @time: 16:44
 */
@Service
public class PoiServiceImpl extends ServiceImpl<PoiMapper, Product> implements PoiService {

    @Override
    public Map<String, Object> importExcelData(String realPath, MultipartFile file, String year) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> map = ExcelReadUtil.getExcelData(file, realPath, year);
        LinkedList<Product> goodsDetails = (LinkedList<Product>) map.get("list");
        if (null == goodsDetails || goodsDetails.size() <= 0) {
            result.put("flag", false);
            result.put("msg", (String) map.get("msg"));
            return result;
        } else {
            int i = baseMapper.batchInsert(goodsDetails);
            if (i > 0) {
                result.put("flag", true);
                result.put("msg", "添加成功!");
            } else {
                result.put("flag", false);
                result.put("msg", "添加失败!");
            }
            return result;
        }
    }

    @Override
    public List<Product> searchProducts() {
        return baseMapper.selectList(null);
    }
}
